﻿using System;
using NDde.Server;

namespace OpenWealth.QuikDataProvider.DDE
{
    public class DDEServer : DdeServer
    {
        private static ILog l = Core.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        //имя сервера по-умолчанию
        private const string defaultServer = "OpenWealth";

        //имя топика по-умолчанию
        private const string defaultTopic = "[ALL_DEAL]ALL_DEAL";

        public delegate void ConnectionEventDelegate(NDde.Server.DdeConversation conv);

        public delegate void PokeEventDelegate(
            NDde.Server.DdeConversation conv, string item, byte[] data, int format);

        public DDEServer()
            : base(defaultServer)
        {
            l.Debug("DDEServer service name" + defaultServer);
        }

        protected override void OnAfterConnect(NDde.Server.DdeConversation conversation)
        {
            base.OnAfterConnect(conversation);
            l.Debug(String.Format("Topic : {0} has been connected!", conversation.Topic));
        }

        protected override void OnDisconnect(NDde.Server.DdeConversation conversation)
        {
            base.OnDisconnect(conversation);
            l.Debug(String.Format("Topic : {0} has been disconnected!", conversation.Topic));
            
        }

        protected override PokeResult OnPoke(NDde.Server.DdeConversation conversation, string item, byte[] data, int format)
        {
            l.Debug("Topic : " + conversation.Topic + " contains following data: " + data);

            switch (conversation.Topic)
            {
                case defaultTopic:
                    AllDealQueue.Add(data);
                    break;
            }

            return PokeResult.Processed;
        }
    }
}
